package Admin;

import GroupClass.ConnectRMI;
import Project_etow.EtowInterface;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import javax.swing.JOptionPane;

public class InsertUnit extends javax.swing.JDialog {

	private String idFlor;

    public String getIdFlor() {
		return idFlor;
	}

	public void setIdtow(String idFloor) {
		this.idFlor = idFloor;
	}
	
    private String checkvisable;
	
	public String getCheckvisable() {
		return checkvisable;
	}

	public void setCheckvisable(String checkvisable) {
		this.checkvisable = checkvisable;
	}
	
    public InsertUnit(java.awt.Frame parent, boolean modal,String idFlor1) {
        super(parent, modal);
        idFlor = idFlor1;
        initComponents();
    }
	
    ConnectRMI connectRMI = new ConnectRMI();
    public InsertUnit(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    	lblFloorID = new javax.swing.JLabel();
        lblUnitName = new javax.swing.JLabel();
        lblUnitPrice = new javax.swing.JLabel();
        txtFFloorID = new javax.swing.JTextField();
        txtFUnitName = new javax.swing.JTextField();
        txtFUnitPrice = new javax.swing.JTextField();
        btnSubmit = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        lblBillId = new javax.swing.JLabel();
        txtFBillId = new javax.swing.JTextField();
       

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        lblFloorID.setText("FloorID");
        lblBillId.setText("Bill_ID");
        lblUnitName.setText("UnitName");
        txtFFloorID.setText(idFlor);
        txtFFloorID.setEnabled(false);
        lblUnitPrice.setText("UnitPrice");

        btnSubmit.setText("Submit");
        btnSubmit.setActionCommand("Submit");
        btnSubmit.addActionListener(new ActionListener() {			
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					try {
					    Registry registry = LocateRegistry.getRegistry(connectRMI.host,connectRMI.port);
	                                        EtowInterface obj=(EtowInterface)registry.lookup(connectRMI.regis);
			
						int floorID;
						
						String decorate= "0";
						
						
						
						floorID = Integer.parseInt(txtFFloorID.getText());
						String unitName = txtFUnitName.getText();
						String unitPrice = txtFUnitPrice.getText();
						String billId = txtFBillId.getText();
						
						if (unitName.length() == 0) {
							JOptionPane.showMessageDialog(null, "Name Unit Not Null");
						}
						else if(unitPrice.length() == 0){
							JOptionPane.showMessageDialog(null, "Price Not Null");
						}				
						else if (unitName.length() != 0 && unitPrice.length() != 0 ){
						    String result = obj.InsertUnit(floorID,unitName, unitPrice, decorate, billId);
						    if (result.equals("Insert")) {
								JOptionPane.showMessageDialog(null, "Insert Successful");								
								setCheckvisable("close");
								setVisible(false);
							}
							else {
								JOptionPane.showMessageDialog(null, "Insert Failed");
							}
						}
						
						}catch (Exception ex) { 
						ex.printStackTrace();
						}				
		}
	});

        btnReset.setText("Reset");
        btnReset.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				txtFUnitName.setText("");
				txtFUnitPrice.setText("");
			}
		});

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				setCheckvisable("close");
				setVisible(false);
			}
		});

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(61, 61, 61)
                        .addComponent(btnSubmit)
                        .addGap(30, 30, 30)
                        .addComponent(btnReset)
                        .addGap(27, 27, 27)
                        .addComponent(btnCancel))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(52, 52, 52)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblFloorID)
                            .addComponent(lblUnitName)
                            .addComponent(lblUnitPrice)
                            .addComponent(lblBillId))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtFFloorID, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
                            .addComponent(txtFUnitName, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
                            .addComponent(txtFUnitPrice, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
                            .addComponent(txtFBillId))))
                .addContainerGap(60, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblFloorID)
                    .addComponent(txtFFloorID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblUnitName)
                    .addComponent(txtFUnitName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblUnitPrice)
                    .addComponent(txtFUnitPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblBillId)
                    .addComponent(txtFBillId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(48, 48, 48)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSubmit)
                    .addComponent(btnReset)
                    .addComponent(btnCancel))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(InsertUnit.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(InsertUnit.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(InsertUnit.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(InsertUnit.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
            	InsertUnit dialog = new InsertUnit(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnSubmit;
    private javax.swing.JLabel lblBillId;
    private javax.swing.JLabel lblFloorID;
    private javax.swing.JLabel lblUnitName;
    private javax.swing.JLabel lblUnitPrice;
    private javax.swing.JTextField txtFBillId;
    private javax.swing.JTextField txtFFloorID;
    private javax.swing.JTextField txtFUnitName;
    private javax.swing.JTextField txtFUnitPrice;
    // End of variables declaration
	}
